package com.anolesoft.yr.mbus.part.service.impl;

import com.anolesoft.yr.exception.ServiceException;
import com.anolesoft.yr.mbus.part.dto.PartTypeDTO;
import com.anolesoft.yr.mbus.part.entity.PartTypeDO;
import com.anolesoft.yr.mbus.part.mapper.PartTypeMapper;
import com.anolesoft.yr.mbus.part.service.PartTypeService;
import com.anolesoft.yr.util.CommonUtils;
import com.anolesoft.yr.util.CustomBeanUtils;
import com.anolesoft.yr.util.PageData;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author zhj
 * @date 2020/8/11 11:38
 */
@Service("partTypeServiceImpl")
public class PartTypeServiceImpl implements PartTypeService {
    @Autowired
    private PartTypeMapper partTypeMapper;

    private void checkUnique(PartTypeDTO partTypeDTO, boolean isModify) {
        String objuid = partTypeDTO.getObjuid();
        String typeCode = partTypeDTO.getTypeCode();
        String typeName = partTypeDTO.getTypeName();
        if (isModify) {
            if (partTypeMapper.checkUniqueForTypeCodeUpdate(typeCode, objuid) > 0) {
                throw new ServiceException("类别编号已存在");
            }
            if (partTypeMapper.checkUniqueForTypeNameUpdate(typeName, objuid) > 0) {
                throw new ServiceException("类别名称已存在");
            }
        } else {
            if (partTypeMapper.checkUniqueForTypeCode(typeCode) > 0) {
                throw new ServiceException("类别编号已存在");
            }
            if (partTypeMapper.checkUniqueForTypeName(typeName) > 0) {
                throw new ServiceException("类别名称已存在");
            }
        }
    }

    @Transactional
    @Override
    public int create(PartTypeDTO partTypeDTO) {
        checkUnique(partTypeDTO, false);
        return partTypeMapper.insert(PartTypeDO.builder()
                .objuid(partTypeDTO.getObjuid())
                .typeCode(partTypeDTO.getTypeCode())
                .typeName(partTypeDTO.getTypeName())
                .factoryCode(partTypeDTO.getFactoryCode())
                .remark(partTypeDTO.getRemark())
                .modifyUserUid(partTypeDTO.getModifyUserUid())
                .createUserUid(partTypeDTO.getCreateUserUid())
                .createTime(partTypeDTO.getCreateTime())
                .deleteFlag(partTypeDTO.getDeleteFlag())
                .modifyTime(partTypeDTO.getModifyTime())
                .build());
    }

    @Transactional
    @Override
    public int modify(PartTypeDTO partTypeDTO) {
        checkUnique(partTypeDTO, true);
        PartTypeDO partTypeDO = partTypeMapper.selectOneById(partTypeDTO.getObjuid());
        if (partTypeDO == null) {
            partTypeDO = PartTypeDO.builder().build();
        }
        partTypeDO.setCreateTime(partTypeDO.getCreateTime());
        partTypeDO.setCreateUserUid(partTypeDO.getCreateUserUid());
        partTypeDO.setDeleteFlag(partTypeDO.getDeleteFlag());
        partTypeDO.setFactoryCode(partTypeDO.getFactoryCode());
        partTypeDO.setModifyTime(partTypeDO.getModifyTime());
        partTypeDO.setModifyUserUid(partTypeDO.getModifyUserUid());
        partTypeDO.setObjuid(partTypeDO.getObjuid());
        partTypeDO.setRemark(partTypeDO.getRemark());
        partTypeDO.setTypeCode(partTypeDTO.getTypeCode());
        partTypeDO.setTypeName(partTypeDTO.getTypeName());
        return partTypeMapper.update(partTypeDO);
    }

    @Transactional
    @Override
    public int remove(String id) {
        return partTypeMapper.delete(id);
    }

    @Override
    public int removeBatch(List<String> ids) {
        int res = 0;
        for (String id : ids) {
            res += partTypeMapper.delete(id);
        }
        return res;
    }

    @Override
    public List<PartTypeDTO> selectAll() {
        return partTypeMapper.selectAll().stream().map(t -> CustomBeanUtils.partTypeDO2DTO(t)).collect(Collectors.toList());
    }

    @Override
    public PageData<List<PartTypeDTO>> selectAll(Integer limit, Integer curr) {
        //return partTypeMapper.selectAll(new Page<>(curr, limit)).stream().map(t -> do2dto(t)).collect(Collectors.toList());

        Page<PartTypeDO> page = new Page<>(curr, limit);
        List<PartTypeDO> partTypeDOS = partTypeMapper.selectPageAll(page);
        List<PartTypeDTO> partTypeDTOS = partTypeDOS.stream().map(t -> CustomBeanUtils.partTypeDO2DTO(t)).collect(Collectors.toList());
        return new PageData<>(((int) page.getTotal()), partTypeDTOS);
    }

    @Override
    public Optional<PartTypeDTO> selectOneById(String id) {
        PartTypeDO partTypeDO = partTypeMapper.selectOneById(id);
        if (partTypeDO != null) {
            return Optional.of(CustomBeanUtils.partTypeDO2DTO(partTypeDO));
        }
        return Optional.empty();
    }

    @Override
    public List<PartTypeDTO> selectList(PartTypeDTO partTypeDTO) {
        PartTypeDO partTypeDO = buildCondition(partTypeDTO);
        return partTypeMapper.selectList(partTypeDO).stream().map(t -> CustomBeanUtils.partTypeDO2DTO(t)).collect(Collectors.toList());
    }

    @Override
    public PageData<List<PartTypeDTO>> selectList(PartTypeDTO partTypeDTO, Integer limit, Integer curr) {
        PartTypeDO partTypeDO = buildCondition(partTypeDTO);
        //return partTypeMapper.selectList(partTypeDO, new Page<>(curr, limit)).stream().map(t -> do2dto(t)).collect(Collectors.toList());
        Page<PartTypeDO> page = new Page<>(curr, limit);
        List<PartTypeDO> partTypeDOS = partTypeMapper.selectPageList(partTypeDO, page);
        List<PartTypeDTO> partTypeDTOS = partTypeDOS.stream().map(t -> CustomBeanUtils.partTypeDO2DTO(t)).collect(Collectors.toList());
        return new PageData<>(((int) page.getTotal()), partTypeDTOS);
    }

    @Override
    public PageData<List<PartTypeDTO>> selectComboxList(PartTypeDTO partTypeDTO, Integer limit, Integer curr) {
        PartTypeDO partTypeDO = PartTypeDO.builder()
                .typeName(CommonUtils.stringNull(partTypeDTO.getTypeName()))
                .typeCode(CommonUtils.stringNull(partTypeDTO.getTypeCode()))
                .build();
        Page<PartTypeDO> page = new Page<>(curr, limit);
        List<PartTypeDO> partTypeDOS = partTypeMapper.selectComboxList(partTypeDO, page);
        List<PartTypeDTO> partTypeDTOS = partTypeDOS.stream().map(CustomBeanUtils::partTypeDO2DTO).collect(Collectors.toList());
        return new PageData<>(((int) page.getTotal()), partTypeDTOS);
    }

    private PartTypeDO buildCondition(PartTypeDTO partTypeDTO) {
        return PartTypeDO.builder()
                .createTime(partTypeDTO.getCreateTime())
                .createUserUid(partTypeDTO.getCreateUserUid())
                .deleteFlag(partTypeDTO.getDeleteFlag())
                .factoryCode(partTypeDTO.getFactoryCode())
                .modifyTime(partTypeDTO.getModifyTime())
                .modifyUserUid(partTypeDTO.getModifyUserUid())
                .objuid(partTypeDTO.getObjuid())
                .remark(partTypeDTO.getRemark())
                .typeCode(CommonUtils.warpStringNull(partTypeDTO.getTypeCode()))
                .typeName(CommonUtils.warpStringNull(partTypeDTO.getTypeName()))
                .build();

    }

//    private PartTypeDTO do2dto(PartTypeDO t) {
//        return PartTypeDTO.builder()
//                .createTime(t.getCreateTime())
//                .createUserUid(t.getCreateUserUid())
//                .deleteFlag(t.getDeleteFlag())
//                .factoryCode(t.getFactoryCode())
//                .modifyTime(t.getModifyTime())
//                .modifyUserUid(t.getModifyUserUid())
//                .objuid(t.getObjuid())
//                .remark(t.getRemark())
//                .typeCode(t.getTypeCode())
//                .typeName(t.getTypeName())
//                .build();
//    }
}
